<%@ include file="/html/common/init.jsp" %>

<%@ page import="com.spreedairy.util.CategoryUtil" %>

<%
ShoppingCategory category = (ShoppingCategory)request.getAttribute("SHOPPING_CATEGORY");

long categoryId = BeanParamUtil.getLong(category, request, "categoryId", ShoppingCategoryConstants.DEFAULT_PARENT_CATEGORY_ID);
%>

<aui:form method="post" name="fm">
	<liferay-ui:header title="categories"/>

	<%
	PortletURL portletURL = renderResponse.createRenderURL();

	portletURL.setParameter("struts_action", "/item/select_category");
	portletURL.setParameter("categoryId", String.valueOf(categoryId));

	List<String> headerNames = new ArrayList<String>();

	headerNames.add("category");
	headerNames.add("categories");
	headerNames.add("items");
	headerNames.add(StringPool.BLANK);

	SearchContainer searchContainer = new SearchContainer(renderRequest, null, null, SearchContainer.DEFAULT_CUR_PARAM, SearchContainer.DEFAULT_DELTA, portletURL, headerNames, null);

	int total = ShoppingCategoryLocalServiceUtil.getCategoriesCount(scopeGroupId, categoryId);

	searchContainer.setTotal(total);

	List results = ShoppingCategoryLocalServiceUtil.getCategories(scopeGroupId, categoryId, searchContainer.getStart(), searchContainer.getEnd());

	searchContainer.setResults(results);

	List resultRows = searchContainer.getResultRows();

	for (int i = 0; i < results.size(); i++) {
		ShoppingCategory curCategory = (ShoppingCategory)results.get(i);

		curCategory = curCategory.toEscapedModel();

		ResultRow row = new ResultRow(curCategory, curCategory.getCategoryId(), i);

		PortletURL rowURL = renderResponse.createRenderURL();

		rowURL.setParameter("struts_action", "/item/select_category");
		rowURL.setParameter("categoryId", String.valueOf(curCategory.getCategoryId()));

		// Statistics

		List subcategoryIds = new ArrayList();

		subcategoryIds.add(new Long(curCategory.getCategoryId()));

		ShoppingCategoryLocalServiceUtil.getSubcategoryIds(subcategoryIds, scopeGroupId, curCategory.getCategoryId());

		int categoriesCount = subcategoryIds.size() - 1;
		int itemsCount = ShoppingItemLocalServiceUtil.getCategoriesItemsCount(scopeGroupId, subcategoryIds);

		// Action

		StringBundler sb = new StringBundler(7);
		sb.append("<a href=\"javascript:void(0)\" onclick=\"");
		sb.append("opener.");
		sb.append(renderResponse.getNamespace());
		sb.append("selectCategory('");
		sb.append(curCategory.getCategoryId());
		sb.append("', '");
		sb.append(UnicodeFormatter.toString(curCategory.getName()));
		sb.append("'); window.close();");
		sb.append("\" >");

		// Name and description

		if (Validator.isNotNull(curCategory.getDescription())) {
			row.addText(sb.toString()+curCategory.getName().concat("<br />").concat(curCategory.getDescription())+"</a>");
		}
		else {
			row.addText(sb.toString()+curCategory.getName()+"</a>");
		}
		
		row.addText(sb.toString()+String.valueOf(categoriesCount), sb.toString()+"</a>");
		row.addText(sb.toString()+String.valueOf(itemsCount), sb.toString()+"</a>");
		
		//row.addButton("right", SearchEntry.DEFAULT_VALIGN, LanguageUtil.get(pageContext, "choose"), sb.toString());

		 // start-my.....
		String showDiv = "<a href=\"javascript:void(0)\" onClick=\"showDiv('rowId"+curCategory.getCategoryId()+"');\">More Child</a>";
		row.addText(showDiv, rowURL);
        // end-my.......

		// Add result row

		resultRows.add(row);
        
		// start-my ...........
		int count = ShoppingCategoryLocalServiceUtil.getCategoriesCount(scopeGroupId, curCategory.getCategoryId());
		if(count <= 0) continue;
		ResultRow row1 = new ResultRow(curCategory, curCategory.getCategoryId(), i);
		
		String subCategoryList = CategoryUtil.getSubCategoryPopupList(scopeGroupId, curCategory.getCategoryId(), renderResponse.getNamespace());
		
		row1.addText("left", "top", 6, subCategoryList);
		resultRows.add(row1);
		
		// End-my ..........
	}
	%>

	<liferay-ui:search-iterator searchContainer="<%= searchContainer %>" />
</aui:form>

<script type="text/javascript">
function showDiv(id) {
	var e = document.getElementById(id);
	if (e.style.display == 'none') {
	   e.style.display = 'block';
    }
    else if(e.style.display == 'block'){
       e.style.display = 'none';
    }
}
</script>
	
	
	
	
	
	
